/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pt.mleiria.app.web.bean;

import java.util.HashSet;
import java.util.Set;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import mleiria.pt.math.Tabuada;
import mleiria.pt.math.TabuadaGenerator;
import pt.mleiria.app.web.voice.MotherVoice;
import pt.mleiria.bo.games.TabuadaScoreEJB;
import pt.mleiria.vo.SessionObjects;
import pt.mleiria.vo.TabuadaScore;
import pt.mleiria.vo.User;
import pt.mleiria.vo.VoiceCmdVO;

/**
 *
 * @author manuel
 */
@ManagedBean(name = "tabuadaBean")
@SessionScoped
public class TabuadaBean extends MasterBean {
    @EJB
    private TabuadaScoreEJB tsEjb;

    public TabuadaScoreEJB getTsEjb() {
        return tsEjb;
    }

    private String question;
    private String answer;
    private int correctAnswer;
    private int questionCounter = 0;
    private String goodImage = "good.gif";
    private String badImage = "bad.gif";
    private final String smileImage = "smile.jpg";
    private String image;
    private TabuadaScore tabuadaScore;
    private long startTime;
    

    public TabuadaBean() {
        super();
        String content = MotherVoice.sayHi;
        content += ((User)getFromSession(SessionObjects.USER)).getName();
        content += MotherVoice.sayLetsPlay;
        content += MotherVoice.sayTabuada;
        getMotherVoiceEjb().speakMother(VoiceCmdVO.PT, content);
        this.image = smileImage;
        tabuadaScore = (TabuadaScore) getFromSession(SessionObjects.TABUADA_SCORE);
        if (null == tabuadaScore) {
            startTime = System.currentTimeMillis();
            tabuadaScore = new TabuadaScore();
            tabuadaScore
                    .setUser_id(((User) getFromSession(SessionObjects.USER))
                            .getId());
            tabuadaScore
                    .setUsername(((User) getFromSession(SessionObjects.USER))
                            .getUsername());
            setToSession(SessionObjects.TABUADA_SCORE, tabuadaScore);
            // allOfFame = new TabuadaScore[1];

        }
        getLogger(TabuadaBean.class).info("SCORE" + tabuadaScore.toString());
    }

    public String checkAnswer() {

        getLogger(TabuadaBean.class).info("ANSWER:" + answer);
        getLogger(TabuadaBean.class).info("CORRECT ANSWER:" + correctAnswer);
        questionCounter++;
        getLogger(TabuadaBean.class).info("COUNTER:" + questionCounter);
        try {
            if (!answer.isEmpty()) {
                answer = answer.trim();
            }
            if (Integer.parseInt(answer) == correctAnswer) {
                tabuadaScore.setScore(tabuadaScore.getScore() + 1);
                this.image = goodImage;
            } else {
                this.image = badImage;
            }
        } catch (NumberFormatException e) {
            this.image = badImage;
        } catch (Exception e) {
            this.image = goodImage;
        }
        tabuadaScore.setTotalQuestions(questionCounter);
        //if(questionCounter == 10) resetAll();
        if (questionCounter == 10) {
            getLogger(TabuadaBean.class).info("END GAME");
            int scoreTime = (int) (System.currentTimeMillis() - startTime);
            tabuadaScore.setScoreTime(scoreTime/1000);
            resetAll();
            return "tabuadaScore";
        }
        resetAnswer();
        getLogger(TabuadaBean.class).info("SCORE" + tabuadaScore.toString());
        return "";

    }
    private void resetAnswer(){
        this.answer = "";
    }
    private void resetAll() {
        startTime = System.currentTimeMillis();
        this.image = smileImage;
        questionCounter = 0;
        tsEjb.saveScore(tabuadaScore);
        tabuadaScore = new TabuadaScore();
        tabuadaScore.setUser_id(((User) getFromSession(SessionObjects.USER))
                .getId());
        tabuadaScore.setUsername(((User) getFromSession(SessionObjects.USER))
                .getUsername());
        setToSession(SessionObjects.TABUADA_SCORE, tabuadaScore);
        setToSession(SessionObjects.TABUADA_QUESTIONS, new HashSet<String>());
    }
    /**
     * @return the question
     */
    public String getQuestion() {
        Tabuada tabuada = TabuadaGenerator
                .getRandomOperacao(TabuadaGenerator.getRandomTabuada());
        
        Set<String> prevQuestions = (Set<String>)getFromSession(SessionObjects.TABUADA_QUESTIONS);
        if(null == prevQuestions){
            prevQuestions = new HashSet<String>();
            setToSession(SessionObjects.TABUADA_QUESTIONS, prevQuestions);
        }else{
            String tmpQuestion = tabuada.getQuestion();
            while(prevQuestions.contains(tmpQuestion)){
                tabuada = TabuadaGenerator.getRandomOperacao(TabuadaGenerator.getRandomTabuada());
                tmpQuestion = tabuada.getQuestion();
            }
        }
        
        this.question = tabuada.getQuestion();
        this.correctAnswer = tabuada.getResultado();
        return question;
    }

    /**
     * @param question the question to set
     */
    public void setQuestion(String question) {
        this.question = question;
    }

    /**
     * @return the answer
     */
    public String getAnswer() {
        return answer;
    }

    /**
     * @param answer the answer to set
     */
    public void setAnswer(String answer) {
        this.answer = answer;
    }

    /**
     * @return the correctAnswer
     */
    public int getCorrectAnswer() {
        return correctAnswer;
    }

    /**
     * @param correctAnswer the correctAnswer to set
     */
    public void setCorrectAnswer(int correctAnswer) {
        this.correctAnswer = correctAnswer;
    }

    /**
     * @return the questionCounter
     */
    public int getQuestionCounter() {
        return questionCounter;
    }

    /**
     * @param questionCounter the questionCounter to set
     */
    public void setQuestionCounter(int questionCounter) {
        this.questionCounter = questionCounter;
    }

    /**
     * @return the goodImage
     */
    public String getGoodImage() {
        return goodImage;
    }

    /**
     * @param goodImage the goodImage to set
     */
    public void setGoodImage(String goodImage) {
        this.goodImage = goodImage;
    }

    /**
     * @return the badImage
     */
    public String getBadImage() {
        return badImage;
    }

    /**
     * @param badImage the badImage to set
     */
    public void setBadImage(String badImage) {
        this.badImage = badImage;
    }

    /**
     * @return the image
     */
    public String getImage() {
        return image;
    }

    /**
     * @param image the image to set
     */
    public void setImage(String image) {
        this.image = image;
    }

}
